﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TouchControls;

namespace TouchExplorer.Controls
{
    /// <summary>
    /// Interaction logic for FilesToolbar.xaml
    /// </summary>
    public partial class FilesToolbar : UserControl
    {
        public ExplorerViewModel Model
        {
            get
            {
                return DataContext as ExplorerViewModel;
            }
        }

        public FilesToolbar()
        {
            InitializeComponent();
            Loaded += (sender, args) =>
                { 
                    Model.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Model_PropertyChanged);
                };
        }

        void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CurrentDirectory")
            {
                if (Model.CurrentDirectory == null)
                {
                    FolderMenu.IsEnabled = false;
                }
                else
                {
                    FolderMenu.IsEnabled = true;
                }
            }
        }

        private void DisplayList(object sender, RoutedEventArgs e)
        {
            Model.Display = ExplorerViewModel.DisplayType.List;
        }

        private void DisplaySlide(object sender, RoutedEventArgs e)
        {
            Model.Display = ExplorerViewModel.DisplayType.Tiles;
        }

        private void SortItem_Click(object sender, RoutedEventArgs e)
        {
            var menu = sender as TouchMenuItem;
            var parent = menu.ParentMenu;

            Model.SetSorting(parent.Name, menu.CommandParameter.ToString());
        }

        private void CopyAll(object sender, RoutedEventArgs e)
        {
            var elt = Model.Files;
            if (elt.Count() > 0)
                App.Current.Clipboard.AddCopy(elt);
        }

        private void CopySelected(object sender, RoutedEventArgs e)
        {
            var elt = Model.Files.Where(f => f.IsSelected);
            if (elt.Count() > 0)
                App.Current.Clipboard.AddCopy(elt);
        }

        private void CutAll(object sender, RoutedEventArgs e)
        {
            var elt = Model.Files;
            if (elt.Count() > 0)
                App.Current.Clipboard.AddCut(elt);
        }

        private void CutSelected(object sender, RoutedEventArgs e)
        {
            var elt = Model.Files.Where(f => f.IsSelected);
            if (elt.Count() > 0)
                App.Current.Clipboard.AddCut(elt);
        }

        private void DeleteAll(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Do you really want to delete all this files ?", "Delete files", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                MessageBox.Show("Not yet implemented");
            }
            var elt = Model.Files;
            if (elt.Count() > 0)
            {
                
            }
        }

        private void DeleteSelected(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Do you really want to delete this files ?", "Delete files", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                MessageBox.Show("Not yet implemented");
            }
            var elt = Model.Files.Where(f => f.IsSelected);
            if (elt.Count() > 0)
            {

            }
        }

        private void FavAll(object sender, RoutedEventArgs e)
        {
            var elt = Model.Files;
            if (elt.Count() > 0)
            {
                App.Current.Favorites.Add(elt);
            }
        }

        private void FavSelected(object sender, RoutedEventArgs e)
        {
            var elt = Model.Files.Where(f => f.IsSelected);
            if (elt.Count() > 0)
            {
                App.Current.Favorites.Add(elt);
            }
        }
        
        private void FolderCopy(object sender, RoutedEventArgs e)
        {
            App.Current.Clipboard.AddCopy(Model.CurrentDirectory);
        }

        private void FolderCut(object sender, RoutedEventArgs e)
        {
            App.Current.Clipboard.AddCut(Model.CurrentDirectory);
        }

        void FolderMenu_OnMenuOpening(object sender, EventArgs args)
        {
            if (Model.CurrentDirectory == null || !Model.CurrentDirectory.Exists)
            {
                FolderMenuCopy.Visibility = Visibility.Collapsed;
                FolderMenuCut.Visibility = Visibility.Collapsed;
                FolderMenuDel.Visibility = Visibility.Collapsed;
                FolderMenuPaste.Visibility = Visibility.Collapsed;
                if (Model.CurrentDirectory == null)
                    FolderMenuFav.Visibility = Visibility.Collapsed;
            }
            else
            {
                FolderMenuFav.Visibility = Visibility.Visible;
                FolderMenuCopy.Visibility = Visibility.Visible;
                FolderMenuCut.Visibility = Visibility.Visible;
                FolderMenuDel.Visibility = Visibility.Visible;
                FolderMenuPaste.Visibility = Visibility.Visible;
            }
            
        }

        private void FolderFav(object sender, RoutedEventArgs e)
        {
            App.Current.Favorites.Add(Model.CurrentDirectory);
        }

        private void FolderDelete(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Do you really want to delete this folder, and all the files it contains ?", "Delete folder", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                MessageBox.Show("Not yet implemented");
            }
        }

        private void FolderPaste(object sender, RoutedEventArgs e)
        {
            var dir = Model.CurrentDirectory;
            if (dir != null)
            {
                App.Current.Clipboard.Apply(dir.FullPath);
            }
        }
    }

}
